import { Action } from "../core/Action";
import { Tick } from "../core/Tick";
import { ActionStatus } from "../core/BaseNode";

export class Wait extends Action {

    endTime: number = 0;

    constructor(params?: any) {
        super(params);
        params = params || {milliseconds: 0};
        this.name = 'Wait';
        this.title = 'Wait <milliseconds>ms';
        this.properties = params;
        this.endTime = params.milliseconds || 0;
    }

    open(tick: Tick) {
        let startTime = (new Date()).getTime();
        tick.blackboard.set('startTime', startTime, tick.tree.id, this.id);
    }

    
    tick(tick: Tick) : ActionStatus {
        let currentTime = (new Date()).getTime();
        let startTime = tick.blackboard.get('startTime', tick.tree.id, this.id);
        if (currentTime - startTime > this.endTime) {
            return ActionStatus.SUCCESS;
        }
        return ActionStatus.RUNNING;
    }
}